package com.songlanyun.modules.sysbill.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.BillConstant;
import com.songlanyun.common.enums.OrderConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.sysbill.dao.SysBillDao;
import com.songlanyun.modules.sysbill.entity.SysBill;
import com.songlanyun.modules.sysbill.service.SysBillService;


@Service("sysBillService")
public class SysBillServiceImpl extends ServiceImpl<SysBillDao, SysBill> implements SysBillService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer billMode = MapUtil.getInt(params, "bill_mode");//出入帐类型(0:出账 1入账)
        Integer payMode = MapUtil.getInt(params, "pay_mode");//支付方式
        Integer billType = MapUtil.getInt(params, "bill_type");//账单类型
        String businessCode = MapUtil.getStr(params, "business_code");//业务单号
        String thirdPayCode = MapUtil.getStr(params, "third_pay_code");//第三方单号
        String userMobile = MapUtil.getStr(params, "user_mobile");//会员手机号

        IPage<SysBill> page = this.page(
                new Query<SysBill>().getPage(params),
                new LambdaQueryWrapper<SysBill>()
                        .eq(billMode != null, SysBill::getBillMode, billMode)
                        .eq(payMode != null, SysBill::getPayMode, payMode)
                        .eq(billType != null, SysBill::getBillType, billType)
                        .like(StringUtils.isNotBlank(businessCode), SysBill::getBusinessCode, businessCode)
                        .like(StringUtils.isNotBlank(thirdPayCode), SysBill::getThirdPayCode, thirdPayCode)
                        .like(StringUtils.isNotBlank(userMobile), SysBill::getUserMobile, userMobile)
                        .orderByDesc(SysBill::getCreateTime)
        );

        return new PageUtils(page);
    }

    /**
     * 保存入账平台账单
     *
     * @param payMode      支付方式
     * @param billType     账单类型
     * @param money        账单金额
     * @param businessCode 业务单号
     * @param thirdPayCode 第三方单号
     * @param userMobile   会员手机号
     */
    @Override
    public void saveIncomeBill(OrderConstant.PayMode payMode, BillConstant.BillType billType, BigDecimal money,
                               String businessCode, String thirdPayCode, String userMobile) {

        //校验业务编码
        if (verifyCode(businessCode)) {
            SysBill sysBill = new SysBill();
            sysBill.setBillMode(BillConstant.BillMode.INCOME);//入账
            sysBill.setPayMode(payMode);//支付方式
            sysBill.setBillType(billType);//账单类型
            sysBill.setMoney(money);//账单金额
            sysBill.setBusinessCode(businessCode);//业务单号
            sysBill.setThirdPayCode(thirdPayCode);//第三方单号
            sysBill.setUserMobile(userMobile);//会员手机号
            baseMapper.insert(sysBill);
        }

    }

    /**
     * 保存出账平台账单  -- 没有则传""
     *
     * @param payMode         支付方式
     * @param billType        账单类型
     * @param money           账单金额
     * @param businessCode    业务单号
     * @param thirdPayCode    第三方单号
     * @param payee           收款人
     * @param withdrawAccount 提现账号
     * @param withdrawMobile  提现手机号
     * @param shopName        商家名称
     * @param bankName        银行卡名称
     * @param userMobile      会员手机号
     */
    @Override
    public void saveExpenditureBill(OrderConstant.PayMode payMode, BillConstant.BillType billType, BigDecimal money,
                                    String businessCode, String thirdPayCode, String payee,
                                    String withdrawAccount, String withdrawMobile, String shopName, String bankName, String userMobile) {
        //校验业务编码
        if (verifyCode(businessCode)) {
            SysBill sysBill = new SysBill();
            sysBill.setBillMode(BillConstant.BillMode.EXPENDITURE);
            sysBill.setMoney(money);//金额
            sysBill.setPayMode(payMode);//支付方式
            sysBill.setBillType(billType);//账单类型
            sysBill.setBusinessCode(businessCode);//业务单号
            sysBill.setThirdPayCode(thirdPayCode);//第三方单号
            sysBill.setPayee(payee);//收款人
            sysBill.setWithdrawAccount((withdrawAccount));//提现账号
            sysBill.setWithdrawMobile((withdrawMobile));//提现手机号
            sysBill.setShopName(shopName);//商家名称
            sysBill.setBankName(bankName);//银行卡名称
            sysBill.setUserMobile(userMobile);//会员手机号

            baseMapper.insert(sysBill);
        }
    }

    /**
     * 校验业务编号
     *
     * @param businessCode
     * @return
     */
    private boolean verifyCode(String businessCode) {
        int count = this.count(
                new LambdaQueryWrapper<SysBill>()
                        .eq(SysBill::getBusinessCode, businessCode)
        );
        return count <= 0;
    }

}
